package com.htandroid.htandroid.webservice;

import android.util.Xml;

import com.htandroid.htandroid.entity.Inventory;
import com.htandroid.htandroid.entity.Vendor;
import com.htandroid.htandroid.tools.ToolLog;

import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;

public class XMLInventorylist {

    /**
     * 解析评论xml信息
     *
     * @param result
     * @return
     */
    public static ArrayList<Inventory> paraseCommentInfors(String result) {
        ArrayList<Inventory> list = new ArrayList<Inventory>();
        //字符转换成字节流
        InputStream inputStream = new ByteArrayInputStream(result.getBytes());
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(inputStream, "UTF-8");
            int eventType = parser.getEventType();
            Inventory info = new Inventory();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:// 文档开始事件,可以进行数据初始化处理
                        break;
                    case XmlPullParser.START_TAG:// 开始元素事件
                        String name = parser.getName();
                        if (name.equalsIgnoreCase("Inventory")) {
                            info = new Inventory();
                        } else {
                            eventType = parser.next();
                            if (name.equalsIgnoreCase("cinvcode")) {
                                info.setCinvcode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvaddcode")) {
                                info.setCinvaddcode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvname")) {
                                info.setCinvname(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvstd")) {
                                info.setCinvstd(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvccode")) {
                                info.setCinvccode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvcname")) {
                                info.setCinvcname(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cvencode")) {
                                info.setCvencode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvm_unit")) {
                                info.setCinvm_unit(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinva_unit")) {
                                info.setCinva_unit(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("creplaceitem")) {
                                info.setCreplaceitem(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cposition")) {
                                info.setCposition(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("bsale")) {
                                info.setBsale(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("bpurchase")) {
                                info.setBpurchase(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("bself")) {
                                info.setBself(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("bcomsume")) {
                                info.setBcomsume(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("bproducing")) {
                                info.setBproducing(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("bservice")) {
                                info.setBservice(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("baccessary")) {
                                info.setBaccessary(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvexchrate")) {
                                info.setIinvexchrate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("itaxrate")) {
                                info.setItaxrate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvweight")) {
                                info.setIinvweight(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("ivolume")) {
                                info.setIvolume(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvrcost")) {
                                info.setIinvrcost(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvsprice")) {
                                info.setIinvsprice(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvscost")) {
                                info.setIinvscost(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvlscost")) {
                                info.setIinvlscost(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvncost")) {
                                info.setIinvncost(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvadvance")) {
                                info.setIinvadvance(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvbatch")) {
                                info.setIinvbatch(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("isafenum")) {
                                info.setIsafenum(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("itopsum")) {
                                info.setItopsum(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("ilowsum")) {
                                info.setIlowsum(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("ioverstock")) {
                                info.setIoverstock(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvabc")) {
                                info.setCinvabc(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("binvquality")) {
                                info.setBinvquality(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("binvbatch")) {
                                info.setBinvbatch(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("binventrust")) {
                                info.setBinventrust(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("binvoverstock")) {
                                info.setBinvoverstock(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("dsdate")) {
                                info.setDsdate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("dedate")) {
                                info.setDedate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("bfree1")) {
                                info.setBfree1(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("bfree2")) {
                                info.setBfree2(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvdefine1")) {
                                info.setCinvdefine1(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvdefine2")) {
                                info.setCinvdefine2(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvdefine3")) {
                                info.setCinvdefine3(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("i_id")) {
                                info.setI_id(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("binvtype")) {
                                info.setBinvtype(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvmpcost")) {
                                info.setIinvmpcost(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cquality")) {
                                info.setCquality(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvsalecost")) {
                                info.setIinvsalecost(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvscost1")) {
                                info.setIinvscost1(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvscost2")) {
                                info.setIinvscost2(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iinvscost3")) {
                                info.setIinvscost3(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("xximport")) {
                                info.setXximport(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("taxrate")) {
                                info.setTaxrate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("zctype")) {
                                info.setZctype(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("isharemonth")) {
                                info.setIsharemonth(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("shoverper")) {
                                info.setShoverper(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("isnew")) {
                                info.setIsnew(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("binvexchtype")) {
                                info.setBinvexchtype(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvmaker")) {
                                info.setCinvmaker(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvmakerdate")) {
                                info.setCinvmakerdate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvmodify")) {
                                info.setCinvmodify(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cinvmodifydate")) {
                                info.setCinvmodifydate(string2null(parser.getText()));
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:// 结束元素事件
                        if (parser.getName().equalsIgnoreCase("Inventory")) {
                            list.add(info);
                            info = null;
                        }
                        break;
                }
                eventType = parser.next();
            }
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //函数作用是把null 转为空
    public static String string2null(String str) {
        String rtn = "";
        if (str == null || str.equals("null") || str.equals("anyType{}")) {
            rtn = "";
        } else {
            rtn = str;
        }
        return rtn;
    }

}  
